package io.gitee.zhangbinhub.acp.cloud.log.consumer

import io.gitee.zhangbinhub.acp.cloud.constant.AcpCloudLogConstant
import io.gitee.zhangbinhub.acp.cloud.log.AcpCloudLogInfo
import io.gitee.zhangbinhub.acp.core.common.CommonTools
import io.gitee.zhangbinhub.acp.core.common.log.LogFactory
import org.noear.snack4.ONode
import org.noear.solon.cloud.CloudEventHandler
import org.noear.solon.cloud.model.Event

class AcpCloudLogConsumer(
    private val logProcess: LogProcess
) : CloudEventHandler {
    private val logFactory = LogFactory.getInstance(this.javaClass)

    @Throws(Throwable::class)
    override fun handle(event: Event): Boolean {
        try {
            val acpCloudLogInfo: AcpCloudLogInfo = ONode.deserialize(event.content(), AcpCloudLogInfo::class.java)
            var logType = AcpCloudLogConstant.DEFAULT_TYPE
            if (!CommonTools.isNullStr(acpCloudLogInfo.logType)) {
                logType = acpCloudLogInfo.logType
            }
            acpCloudLogInfo.logType = logType
            logProcess.process(acpCloudLogInfo)
        } catch (e: Exception) {
            logFactory.error(e.message, e)
        }
        return true
    }
}